//
//  YCStatus.swift
//  swift微博
//
//  Created by 姚伟聪 on 15/12/21.
//  Copyright © 2015年 姚伟聪. All rights reserved.
//

import UIKit

//这是用户数据模型
class YCStatus: NSObject {

   
    // MARK: - 属性
    /// 创建时间
    var created_at: String?
    
    /// 微博id
    var id: Int64 = 0
    
    /// 微博正文内容
    var text: String?
    
    /// 来源
    var source: String?
    
    /// 微博的配图 [String: AnyObject]
    var pic_urls: [[String: AnyObject]]? {
        //因为如果不转换，每次外面调用都得通过key去取图片地址
        didSet{
            let count = pic_urls?.count
            //如果配图为空，就不用给配图的数组赋值了
            if count == 0{
            
                return
            }
            //初始化一个用来存图片地址的数组
            storePictureURLs = [NSURL]()
            
            //便利图片URL数组，通过key取得图片地址
            for url in pic_urls!{
            //数组取出来是id类型的需要强转
               let picString =  url["thumbnail_pic"] as! String
                
                storePictureURLs?.append(NSURL(string: picString)!)
            }
        
        }
    
    
    }// 数组,里面的元素是字典
    /// 微博配图的NSURL数组.
    var storePictureURLs: [NSURL]?
    /// 转发数
    var reposts_count: Int = 0
    
    /// 评论数
    var comments_count: Int = 0
    
    /// 表态数(赞)
    var attitudes_count: Int = 0
    
    //用户数据
    var users: YCUser?

    init(dict:[String:AnyObject]) {
        
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            
            let dic = value as! [String:AnyObject]
            
            users = YCUser(dic: dic)
            //这里要返回
            return
        }
        
        super.setValue(value, forKey: key)
    }
    
    //加载用户数据
    class func loadStatusData(finish:(result:[YCStatus]? ,error: NSError?)-> Void){
    
        YCNetWorkTool.shardNetWorkTool.loadUserData { (result, error) -> Void in
            
            if error != nil {
            
                return
            }
            
            //这是个数组
            let status = result!["statuses"] as? [[String:AnyObject]]
//            print("\(status![0])")
            //字典转模型，定义空数组用来存模型
            var statusArr = [YCStatus]()
            
            for dic in status! {
            
              
                let statu = YCStatus(dict: dic)
                statusArr.append(statu)
                
            }
            
            
            finish(result: statusArr, error: nil)
        }
    
    
    }
    
    
    
    
}
